﻿<?php
include_once('common/header.php');
if($AppUI->id < 0) {
	$this->_redirect(HOST_VFRIEND);
}
$intType	= $objRequest->getParam('ptsave', 0);
if($intType > 0) {
	$objVfriendUser		= new Vfriend_User();
	$strPassword 		= $objRequest->getParam('phascode', '');
	$strDbPassword		= $objVfriendUser->getPassword($AppUI->email);
	$strChapPassword	= md5(md5(session_id()) . $strDbPassword);	
	switch($intType) {
		case 1:
			$strEmail 		= $objRequest->getParam('pemail', '');
			$intError		= 0;
			if($strEmail && $strPassword == $strChapPassword) {
				IF(isset($_SESSION['st_email'])) unset($_SESSION['st_email']);
				$intError 	= $objVfriendUser->updateEmail($AppUI->id, $strEmail);
				if($intError == 0) {
					$intExpireTime	= time() + (24*60*60*ACTIVE_EXPIRE_TIME);				
					$strSubject		= ACCOUNT_EMAIL_SUB;
					$objView 		= Zend_Registry::get('objView'); 
					$this->view->intHexUserId	= Vfriend_Function::base64E($AppUI->hex);
					$this->view->intExpireTime	= Vfriend_Function::base64E($intExpireTime);
					$this->view->strEmail		= Vfriend_Function::base64E($strEmail);
					$strContent 				= $objView->fetch('email/change-email.tpl');
					$objMail					= new Vfriend_Mail();
					$intSend					= $objMail->send(EMAIL_NOREPLY, EMAIL_NOREPLY_NAME, $strEmail, $strSubject, $strContent);
					if($intSend) {
						$strMessage	= $arrMessage[2];
						$strMessage	= str_replace('#strEmail#', $strEmail, $strMessage);
						Vfriend_Function::setMsg($strMessage);
						$this->_redirect(LINK_ACCOUNT_BASIC);
					}
					else 
						$intError	= 14;
				} else {
					$intError += 34;
				}
			} else {
				$intError	= 1;				
			}	
			if($intError > 0) {
				$_SESSION['st_email'] = $strEmail;
				Vfriend_Function::setError($arrError[$intError]);
				$this->_redirect(LINK_ACCOUNT_BASIC . '?type=1');				
			}
			break;
		case 2:
			$strNewPassword		= $objRequest->getParam('pnewpassword','');
			$strNewPasswordConf	= $objRequest->getParam('pnewpassword_confirm','');			
			if($strPassword == $strChapPassword && $strNewPassword == $strNewPasswordConf) {				
				$objVfriendUser->updatePassword($AppUI->id, md5($strNewPassword), $AppUI->email);
				Vfriend_Function::deleteCookie(COOKIE_VFRIEND_AUTH);				
				if (isset($_SESSION) && $_SESSION) {
					session_regenerate_id();
					foreach ($_SESSION as $key=>$value) {
						if ($key != SESSION_BACKURL) {
							unset($_SESSION[$key]);
						}
					}
				}				
				Vfriend_Function::setMsg($arrMessage[3]);
				$this->_redirect(HOST_VFRIEND);
			} else {
				Vfriend_Function::setError($arrError[1]);	
				$this->_redirect(LINK_ACCOUNT_BASIC . '?type=2');
			}
		case 3:
			$strMobile	= $objRequest->getParam('pmobile', '');
			if(!Vfriend_Function::isValidMobile($strMobile)) {
				$intError = 1;
				$_SESSION['st_mobile'] = $strMobile;
				Vfriend_Function::setError($arrError[3]);
				$this->_redirect(LINK_ACCOUNT_BASIC . '?type=3');
			}
			if($strPassword == $strChapPassword && $intError == 0 && $strMobile != '') {
				if(isset($_SESSION['st_mobile'])) unset($_SESSION['st_mobile']);
				$objVfriendUser->updateMobile($AppUI->id, $strMobile);
				Vfriend_Function::setMsg($arrMessage[4]);
				$this->_redirect(LINK_ACCOUNT_BASIC);
			}
			break;
		case 4:
			$strUsername	= $objRequest->getParam('pusername', '');
			$intError		= 0;
			if($strUsername == '') $intError = 31;
			if(strlen($strUsername) < 5 || strlen($strUsername) > 30) $intError = 32;
			if(!Vfriend_Function::isValidUsername($strUsername)) $intError = 33;
			if($intError == 0) {
				if(isset($_SESSION['st_username'])) unset($_SESSION['st_username']);
				$intError = $objVfriendUser->updateUsername($AppUI->id, $strUsername);
				if($intError == 0) {
					$AppUI->username					= $strUsername;
					$_SESSION['Zend_Auth']['storage']	= $AppUI;
					Vfriend_Function::setMsg($arrMessage[15]);
					$this->_redirect(LINK_ACCOUNT_BASIC);
				}
				else {
					$intError = 34;
				}
			}
			if($intError > 0) {
				$_SESSION['st_username'] = $strUsername;
				Vfriend_Function::setError($arrError[$intError]);
				$this->_redirect(LINK_ACCOUNT_BASIC . '?type=4');
			}
			break;
		case 5:
			$strFirstname = $objRequest->getParam('pfirstname', '');
			$strLastname  = $objRequest->getParam('plastname', '');
			$intError	  = 0;
			if($strFirstname == '') {
				$intError = 4;
			}
			if($strLastname == '') {
				$intError = 5;
			}
			if($strFirstname != '' && strlen($strFirstname) < 2) {
				$intError = 6;
			}
			if($strLastname != '' && strlen($strLastname) < 2) {
				$intError = 7;
			}
			if($intError == 0) {
				if(isset($_SESSION['st_firstname'])) unset($_SESSION['st_firstname']);
				if(isset($_SESSION['st_lastname'])) unset($_SESSION['st_lastname']);
				$objVfriendUser->updateFullName($AppUI->id, $strFirstname, $strLastname);
				Vfriend_Function::setMsg($arrMessage[5]);
				$this->_redirect(LINK_ACCOUNT_BASIC);
			}
			else {
				$_SESSION['st_firstname'] 	= $strFirstname;
				$_SESSION['st_lastname'] 	= $strLastname;
				Vfriend_Function::setError($arrError[$intError]);
				$this->_redirect(LINK_ACCOUNT_BASIC . '?type=5');
			}
			break;
		case 6:
			$strCountry		= $objRequest->getParam('pcountry', '');
			$intProvince	= $objRequest->getParam('pprovince', 0);
			$intDistrict	= $objRequest->getParam('pdistrict', 0);
			$strAddr		= $objRequest->getParam('paddr', '');
			
			$objUser	= $objVfriendUser->getFull($AppUI->id);				
			$objVfriendUser->addUpdateBasic($AppUI->id, $objUser->religion, $objUser->occupation, $objUser->education, $objUser->position, 
											$objUser->marital, $strAddr, $intDistrict, $intProvince, $objUser->state,
											$strCountry, $objUser->personality, $objUser->height, $objUser->weight);
			Vfriend_Function::setMsg($arrMessage[6]);
			// clear cache
			$objCache->clear->deleteUserByEmail($AppUI->email);
			$objCache->clear->deleteUserByUsername($AppUI->username);
			$this->_redirect(LINK_ACCOUNT_BASIC);			
			break;
		case 7:
			$strIntroduce	= $objRequest->getParam('pintroduce', '');
			$strIntroduce 	= Vfriend_Function::stripTags($strIntroduce,0);
			$intError		= 0;
			if( $strIntroduce== '') {
				$intError	= 8;
			}
			if(strlen($strIntroduce) < 10) {
				$intError	= 9;
			}
			if($intError == 0) {
				if(isset($_SESSION['st_introduce'])) unset($_SESSION['st_introduce']);
				$objVfriendUser->updateIntroduce($AppUI->id, $strIntroduce);
				Vfriend_Function::setMsg($arrMessage[7]);
				$this->_redirect(LINK_ACCOUNT_BASIC);
			}else {
				$_SESSION['st_introduce'] 	= $strIntroduce;
				Vfriend_Function::setError($arrError[$intError]);
				$this->_redirect(LINK_ACCOUNT_BASIC . '?type=7');
			}
			break;
		case 8:
			$strPersonality	= $objRequest->getParam('ppersonality', '');
			$strPersonality = Vfriend_Function::stripTags($strPersonality,0);
			$intError		= 0;
			if( $strPersonality== '') {
				$intError	= 10;
			}
			if(strlen($strPersonality) < 5) {
				$intError	= 11;
			}
			if($intError == 0) {
				if(isset($_SESSION['st_personality'])) unset($_SESSION['st_personality']);
				$objVfriendUser->updatePersonality($AppUI->id, $strPersonality);
				Vfriend_Function::setMsg($arrMessage[8]);
				$this->_redirect(LINK_ACCOUNT_BASIC);
			}else {
				$_SESSION['st_personality'] 	= $strPersonality;
				Vfriend_Function::setError($arrError[$intError]);
				$this->_redirect(LINK_ACCOUNT_BASIC . '?type=8');
			}
			break;
			break;
		case 9:
			$intReligion		= $objRequest->getParam('preligion', 0);
			$strReligionOther	= $objRequest->getParam('preligion_other', '');
			$strReligionOther	= strip_tags($strReligionOther);
			$intError			= 0;
			if($intReligion == '') {
				$intError = 12;
				
			}
			if($intReligion == -1) {
				$_SESSION['st_religion'] 		= -1;
				$_SESSION['st_religion_other']  = $strReligionOther;
				if($strReligionOther == '')
					$intError = 13;
				if(strlen($strReligionOther) < 5) 
					$intError = 14;
			}
			if($intError == 0) {
				if(isset($_SESSION['st_religion'])) unset($_SESSION['st_religion']);
				if(isset($_SESSION['st_religion_other'])) unset($_SESSION['st_religion_other']);
				if($intReligion == -1) {
					$objCommon	 = new Vfriend_Common();
					$intReligion = $objCommon->addCategory($AppUI->id, 1, $strReligionOther);
				}
				$objUser	= $objVfriendUser->getFull($AppUI->id);
				$objVfriendUser->addUpdateBasic($AppUI->id, $intReligion, $objUser->occupation, $objUser->education, $objUser->position, $objUser->marital,
												$objUser->addr, $objUser->district, $objUser->province, $objUser->state,
												$objUser->country, $objUser->personality, $objUser->height, $objUser->weight);
				// clear cache
				$objCache->clear->deleteUserByEmail($AppUI->email);
				$objCache->clear->deleteUserByUsername($AppUI->username);
				Vfriend_Function::setMsg($arrMessage[9]);
				$this->_redirect(LINK_ACCOUNT_BASIC);
				
			}
			else {
				Vfriend_Function::setError($arrError[$intError]);
				$this->_redirect(LINK_ACCOUNT_BASIC . '?type=9');
			}
			break;
		case 10:
			$intEducation		= $objRequest->getParam('peducation', 0);
			$strEducationOther	= $objRequest->getParam('peducation_other', '');
			$strEducationOther	= strip_tags($strEducationOther);
			$intError			= 0;
			if($intEducation == '') {
				$intError = 15;
				
			}
			if($intEducation == -1) {
				$_SESSION['st_education'] = -1;
				$_SESSION['st_education_other'] = $strEducationOther;
				if($strEducationOther == '')
					$intError = 16;
				if(strlen($strEducationOther) < 5) 
					$intError = 17;
			}
			if($intError == 0) {
				if(isset($_SESSION['st_education'])) unset($_SESSION['st_education']);
				if(isset($_SESSION['st_education_other'])) unset($_SESSION['st_education_other']);
				if($intEducation == -1) {
					$objCommon	 	= new Vfriend_Common();
					$intEducation 	= $objCommon->addCategory($AppUI->id, 4, $strEducationOther);
				}
				$objUser	= $objVfriendUser->getFull($AppUI->id);				
				$objVfriendUser->addUpdateBasic($AppUI->id, $objUser->religion, $objUser->occupation, $intEducation, $objUser->position, $objUser->marital,
												$objUser->addr, $objUser->district, $objUser->province, $objUser->state,
												$objUser->country, $objUser->personality, $objUser->height, $objUser->weight);
				Vfriend_Function::setMsg($arrMessage[10]);
				// clear cache
				$objCache->clear->deleteUserByEmail($AppUI->email);
				$objCache->clear->deleteUserByUsername($AppUI->username);
				$this->_redirect(LINK_ACCOUNT_BASIC);
				
			}
			else {
				Vfriend_Function::setError($arrError[$intError]);
				$this->_redirect(LINK_ACCOUNT_BASIC . '?type=10');
			}
			break;
		case 11:
			$intOccupation		= $objRequest->getParam('poccupation', 0);
			$strOccupationOther	= $objRequest->getParam('poccupation_other', '');
			$strOccupationOther	= strip_tags($strOccupationOther);
			$intError			= 0;
			if($intOccupation == '') {
				$intError = 18;
				
			}
			if($intOccupation == -1) {
				$_SESSION['st_occupation'] = -1;
				$_SESSION['st_occupation_other'] = $strOccupationOther;
				if($strOccupationOther == '')
					$intError = 19;
				if(strlen($strOccupationOther) < 5) 
					$intError = 20;
			}
			if($intError == 0) {
				if(isset($_SESSION['st_occupation'])) unset($_SESSION['st_occupation']);
				if(isset($_SESSION['st_occupation_other'])) unset($_SESSION['st_occupation_other']);
				if($intOccupation == -1) {
					$objCommon	 	= new Vfriend_Common();
					$intOccupation 	= $objCommon->addCategory($AppUI->id, 5, $strOccupationOther);
				}
				$objUser	= $objVfriendUser->getFull($AppUI->id);				
				$objVfriendUser->addUpdateBasic($AppUI->id, $objUser->religion, $intOccupation, $objUser->education, $objUser->position, 
												$objUser->marital, $objUser->addr, $objUser->district, $objUser->province, $objUser->state,
												$objUser->country, $objUser->personality, $objUser->height, $objUser->weight);
				Vfriend_Function::setMsg($arrMessage[11]);
				// clear cache
				$objCache->clear->deleteUserByEmail($AppUI->email);
				$objCache->clear->deleteUserByUsername($AppUI->username);
				$this->_redirect(LINK_ACCOUNT_BASIC);
				
			}
			else {
				Vfriend_Function::setError($arrError[$intError]);
				$this->_redirect(LINK_ACCOUNT_BASIC . '?type=11');
			}
			break;
		case 12:
			$intPosition		= $objRequest->getParam('pposition', 0);
			$strPositionOther	= $objRequest->getParam('pposition_other', '');
			$strPositionOther	= strip_tags($strPositionOther);
			$intError			= 0;
			if($intPosition == '') {
				$intError = 21;
				
			}
			if($intPosition == -1) {
				$_SESSION['st_position'] = -1;
				$_SESSION['st_position_other'] = $strPositionOther;
				if($strPositionOther == '')
					$intError = 22;
				if(strlen($strPositionOther) < 5) 
					$intError = 23;
			}
			if($intError == 0) {
				if(isset($_SESSION['st_position'])) unset($_SESSION['st_position']);
				if(isset($_SESSION['st_position_other'])) unset($_SESSION['st_position_other']);
				if($intPosition == -1) {
					$objCommon	 	= new Vfriend_Common();
					$intPosition 	= $objCommon->addCategory($AppUI->id, 6, $strPositionOther);
				}
				$objUser	= $objVfriendUser->getFull($AppUI->id);				
				$objVfriendUser->addUpdateBasic($AppUI->id, $objUser->religion, $objUser->occupation, $objUser->education, $intPosition, 
												$objUser->marital, $objUser->addr, $objUser->district, $objUser->province, $objUser->state,
												$objUser->country, $objUser->personality, $objUser->height, $objUser->weight);
				Vfriend_Function::setMsg($arrMessage[12]);
				// clear cache
				$objCache->clear->deleteUserByEmail($AppUI->email);
				$objCache->clear->deleteUserByUsername($AppUI->username);
				$this->_redirect(LINK_ACCOUNT_BASIC);				
			}
			else {
				Vfriend_Function::setError($arrError[$intError]);
				$this->_redirect(LINK_ACCOUNT_BASIC . '?type=12');
			}
			break;
		case 13:
			$intMarital			= $objRequest->getParam('pmarital', 0);
			$strMaritalOther	= $objRequest->getParam('pmarital_other', '');
			$strMaritalOther	= strip_tags($strMaritalOther);
			$intError			= 0;
			if($intMarital == '') {
				$intError = 22;
				
			}
			if($intMarital == -1) {
				$_SESSION['st_marital'] = -1;
				$_SESSION['st_marital_other'] = $strMaritalOther;
				if($strMaritalOther == '')
					$intError = 23;
				if(strlen($strMaritalOther) < 5) 
					$intError = 24;
			}
			if($intError == 0) {
				if(isset($_SESSION['st_marital'])) unset($_SESSION['st_marital']);
				if(isset($_SESSION['st_marital_other'])) unset($_SESSION['st_marital_other']);
				if($intMarital == -1) {
					$objCommon	 	= new Vfriend_Common();
					$intMarital 	= $objCommon->addCategory($AppUI->id, 2, $strMaritalOther);
				}
				$objUser	= $objVfriendUser->getFull($AppUI->id);				
				$objVfriendUser->addUpdateBasic($AppUI->id, $objUser->religion, $objUser->occupation, $objUser->education, $objUser->position, 
												$intMarital, $objUser->addr, $objUser->district, $objUser->province, $objUser->state,
												$objUser->country, $objUser->personality, $objUser->height, $objUser->weight);
				Vfriend_Function::setMsg($arrMessage[13]);
				// clear cache
				$objCache->clear->deleteUserByEmail($AppUI->email);
				$objCache->clear->deleteUserByUsername($AppUI->username);
				$this->_redirect(LINK_ACCOUNT_BASIC);				
			}
			else {
				Vfriend_Function::setError($arrError[$intError]);
				$this->_redirect(LINK_ACCOUNT_BASIC . '?type=13');
			}
			break;
		case 14:
			$intHeight	= $objRequest->getParam('pheight', 0);
			$intWeight	= $objRequest->getParam('pweight', 0);
			$intError	= 0;
			if($intHeight == '') $intError = 25;
			if($intHeight != '' && (int)$intHeight < 50) $intError = 26;
			if($intWeight == '') $intError = 27;
			if($intWeight != '' && (int)$intWeight < 30) $intError = 28;
			if($intError == 0) {
				if(isset($_SESSION['st_height'])) unset($_SESSION['st_height']);
				if(isset($_SESSION['st_weight'])) unset($_SESSION['st_weight']);
				$objUser	= $objVfriendUser->getFull($AppUI->id);				
				$objVfriendUser->addUpdateBasic($AppUI->id, $objUser->religion, $objUser->occupation, $objUser->education, $objUser->position, 
												$objUser->marital, $objUser->addr, $objUser->district, $objUser->province, $objUser->state,
												$objUser->country, $objUser->personality, $intHeight, $intWeight);
				Vfriend_Function::setMsg($arrMessage[14]);
				// clear cache
				$objCache->clear->deleteUserByEmail($AppUI->email);
				$objCache->clear->deleteUserByUsername($AppUI->username);
				$this->_redirect(LINK_ACCOUNT_BASIC);	
			} else {
				$_SESSION['st_height'] = $intHeight;
				$_SESSION['st_weight'] = $intWeight;
				Vfriend_Function::setError($arrError[$intError]);
				$this->_redirect(LINK_ACCOUNT_BASIC . '?type=14');
			}
			break;
		default:
			break;
	}
	
}
else {
	Vfriend_Function::setError($arrError[2]);
	$strUrl = LINK_ACCOUNT_BASIC . '?type='.$intType;	
	$this->_redirect($strUrl);	
}
?>